
public class MySingleLinkedList implements IMySingleList{
    static class ListNode {
        public int value;
        public ListNode next;
        public ListNode(int value) {
            this.value = value;
        }
    }
    //定义头节点
    public ListNode head;

    public void creatList() {
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(30);
        ListNode node4 = new ListNode(40);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode cur = head;
        //当head没有数据时，cur.next为空（会出现空指针异常）,此时相当于头插
        if(head == null) {
            addFirst(data);
            return;
        }
        ListNode node = new ListNode(data);
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override//插入到指定下标位置，头节点位置为1
    public void addIndex(int index, int data) {
        if(index == 1) {
            addFirst(data);
            return;
        }
        if(index == size() + 1) {
            addLast(data);
            return;
        }
        checkIndex(index);
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
    }
    private void checkIndex(int index) {
        if(index <= 0 || index > size()) {
            throw new IndexException("下标位置不合法！");
        }
    }

    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = index - 2;
        while (count != 0) {
            cur = cur.next;
            count--;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //当链表为空时，即无法删除
        if(head == null) {
            System.out.println("链表为空，无法删除！");
            return;
        }
        //当链表第一个为要删除的数据，只需要移动head指向
        if(head.value == key) {
            head = head.next;
            return;
        }
        //定义要删除节点的前一个节点
        ListNode cur = search(key);
        //当没有找到要删除的数据（cur也就是null），直接返回
        if(cur == null) {
            System.out.println("链表中没有你要找的数据！");
            return;
        }
        ListNode delete = cur.next;
        cur.next = delete.next;

    }

    /**
     * 找到key的前驱
     * @param key
     * @return
     */
    private ListNode search(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        //当链表为空时，即无法删除
        if(head == null) {
            System.out.println("链表为空，无法删除！");
            return;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while (cur != null) {
            if(cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = prev.next;
                cur = cur.next;
            }
        }
        if(head.value == key) {
            head = head.next;
        }
    }

    @Override
    public int size() {
        int usedSize = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            usedSize++;
        }
        return usedSize;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        ListNode curN = head;
        while (cur != null) {
            cur.value = 0;
            curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        //this.head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.value +" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display(ListNode headN) {
        ListNode cur = headN;
        while(cur != null) {
            System.out.print(cur.value +" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void reverseList() {
        //当链表没有节点，返回空
        if(head == null) {
            return;
        }
        //链表只有一个节点，返回本身
        if(head.next == null) {
            return;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
    }
    public ListNode middleNode1() {
        int count = size()/2;
        ListNode cur = head;
        while (count != 0) {
            cur = cur.next;
            count--;
        }
        return cur;
    }
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public int kthToLast(int k) {
        if(k<0 || head == null) {
            System.out.println("k值不合法");
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = k - 1;
        while(count > 0) {
            fast = fast.next;
            if(fast == null) {
                return -1;
            }
            count--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.value;
        /*if(k<0 || k > size()) {
            System.out.println("k值不合法");
            return -1;
        }
        ListNode cur = head;
        int count = size() -k;
        while ( count > 0) {
            cur = cur.next;
            count--;
        }
        return cur.value;*/
    }
    public ListNode partition(int x) {
        ListNode bStart = null;
        ListNode bEnd = null;
        ListNode aStart = null;
        ListNode aEnd = null;
        ListNode cur = head;
        while (cur != null) {
            if (cur.value < x) {
                if (bStart == null) {
                    bStart = cur;
                    bEnd = cur;
                } else {
                    bEnd.next = cur;
                    bEnd = bEnd.next;
                }
            } else {
                if (aStart == null) {
                    aStart = cur;
                    aEnd = cur;
                }else {
                    aEnd.next = cur;
                    aEnd = aEnd.next;
                }
            }
            cur = cur.next;
        }
        //当没有小于x的值时，第一个链表为空
        if(bStart == null) {
            return aStart;
        }
        //大于等于x的值中最后的节点不一定指向null，可能还指向原链表中的节点，此时需要置为null
        if(aEnd != null) {
            aEnd.next = null;
        }
        bEnd.next = aStart;
        return bStart;
    }
    public boolean chkPalindrome() {
        if (head == null) {
            return true;
        }
        //找到中间节点
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //翻转后半部分链表
        ListNode cur = slow.next;
        slow.next = null;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //让head走到slow，进行比较
        while (head != slow) {
            if(head.value != slow.value) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head =head.next;
            slow = slow.next;
        }
        return true;
    }
    public boolean hasCycle() {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
}
